package com.gensdai.leliang.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.gensdai.leliang.R;
import com.gensdai.leliang.base.BaseFragment;
import com.gensdai.leliang.common.adapter.CartListAdapterRec;
import com.gensdai.leliang.common.bean.ShopCarBean;
import com.gensdai.leliang.remoteInterface.RetrofitFactory;
import com.gensdai.leliang.retrofitUtils.ModelFilteredFactory;
import com.gensdai.leliang.view.Toaster;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by zsxx on 2017/6/22.
 */

public class CartListFragment extends BaseFragment {

    @BindView(R.id.carActBar)
    RelativeLayout carActBar;
    @BindView(R.id.shopcartRec)
    RecyclerView shopcartRec;
    @BindView(R.id.ivSelectAll)
    ImageView ivSelectAll;
    @BindView(R.id.tvCountMoney)
    TextView tvCountMoney;
    @BindView(R.id.tvPostPrice)
    TextView tvPostPrice;
    @BindView(R.id.btnSettle)
    TextView btnSettle;
    @BindView(R.id.rlBottomBar)
    RelativeLayout rlBottomBar;
    Unbinder unbinder;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.editBtn)
    TextView editBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_car, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecycler();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //获取购物车内容

        SelectShopCarRequestStr();
    }



    /**
     * 查看购物车----------------------------------------------------------------------
     */
    private void SelectShopCarRequestStr() {
        //获取edit的值后请求


        Map<String, String> params = new HashMap<>();
//        params.put("userId", share.getString("user_id", ""));   //用户id
        ModelFilteredFactory.compose(RetrofitFactory.getInstance().API().QueryShopCarRequest(params))
                .compose(this.<ShopCarBean>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(new Observer<ShopCarBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ShopCarBean shopCarBean) {
                        if(shopCarBean != null){
                            Toaster.showOneToast("请求到购物车的数据了");
                            attrcartList(shopCarBean);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    private void attrcartList(ShopCarBean shopCarBean){
        if (adapters != null) {
            adapters.clear();
            delegateAdapter.notifyDataSetChanged();
        }

        if (shopCarBean == null && shopCarBean.getShopCarProdList()== null) {
            return;
        }


        LinearLayoutHelper layoutHelper = new LinearLayoutHelper();
        layoutHelper.setDividerHeight(getResources().getDimensionPixelOffset(R.dimen.px10dp));
        if (adapters != null) {
            CartListAdapterRec menuAdapter = new CartListAdapterRec(getActivity(), layoutHelper, shopCarBean.getShopCarProdList());
            adapters.add(menuAdapter);
        }

        delegateAdapter.setAdapters(adapters);
        delegateAdapter.notifyDataSetChanged();

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }



    DelegateAdapter delegateAdapter;
    List<DelegateAdapter.Adapter> adapters;
    //初始化recyclerview
    private void initRecycler() {
        //创建线性布局
        final VirtualLayoutManager layoutManager = new VirtualLayoutManager(getActivity());
        shopcartRec.setLayoutManager(layoutManager);


        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();

        shopcartRec.setRecycledViewPool(viewPool);

        viewPool.setMaxRecycledViews(0, 20);

        delegateAdapter = new DelegateAdapter(layoutManager, false);

        shopcartRec.setAdapter(delegateAdapter);

        adapters = new LinkedList<>();
    }
}
